'use strict';



class AccountCancellationDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("accountCancellation")
        this.handle.createIndex("pID", 1)
        this.handle.createIndex("time", 1)
        this.handle.createIndex({ "pID": 1, "time": -1, "state": -1 })
        this.handle.createIndex({ "pID": 1, "state": -1 })
    }

    newAndSave = async (pID): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            pID: pID,
            time: date,
            state: 0, // 0 unread, 1 read, 2 deleted
            desc: "",
        });
    }

    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default AccountCancellationDao